package {{PackageName}}

import (
	"github.com/gin-gonic/gin"

	"github.com/runningwater/gohub/pkg/app"
	"github.com/runningwater/gohub/pkg/database"
	"github.com/runningwater/gohub/pkg/paginator"
)

func Get(idStr string) ({{VariableName}} {{StructName}}) {
    database.DB.Where("id", idStr).First(&{{VariableName}})
    return
}

func GetBy(field, value string) ({{VariableName}} {{StructName}}) {
    database.DB.Where("? = ?", field, value).First(&{{VariableName}})
    return
}

func All() ({{VariableNamePlural}} []{{StructName}}) {
    database.DB.Find(&{{VariableNamePlural}})
    return 
}

func IsExist(field, value string) bool {
    var count int64
    database.DB.Model({{StructName}}{}).Where("? = ?", field, value).Count(&count)
    return count > 0
}

// Paginate 分页内容
func Paginate(c *gin.Context, pageSize int) ({{VariableNamePlural}} []{{StructName}}, paging paginator.Paging) {
	paging = paginator.Paginate(
		c,
		database.DB.Model({{StructName}}{}),
		&{{VariableNamePlural}},
		app.V1URL(database.TableName(&{{StructName}}{})),
		pageSize,
	)
	return
}
